package view;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.border.EtchedBorder;

import model.Kunde;
import controller.ButtonListener;
import controller.KundenListenMausListener;

public class KundenSuchLeiste extends JInternalFrame implements KundenListeAnzeige,KundenSuchLeser{

	
	
	private class KundenListenZeichner implements ListCellRenderer {
		
		private JLabel item;
		@Override
		public Component getListCellRendererComponent(JList list, Object objectToDraw,
				int index, boolean isSelected, boolean hasFocus) {
			
			if(objectToDraw == null){
				return new JLabel();
			}
			
			Kunde kunde = (Kunde) objectToDraw;
			String toShow = String.valueOf(kunde.getKundennr()) + " " + kunde.getName(); 
			item = new JLabel(toShow);
			
			Color background;
			Color foreground;

			if (isSelected) {
				background = Color.BLUE;
				foreground = Color.WHITE;
			} else {
				background = Color.WHITE;
				foreground = Color.BLACK;
			};
			
			item.setToolTipText("Doppelklick fuer Details");
			item.setOpaque(true);
			item.setBackground(background);
			item.setForeground(foreground);
			item.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
			
			return item;
		}
	}
	
	private static final long serialVersionUID = 5006235599469165472L;

	private MainFrame owner;
	
	private GridBagLayout suchFelderLayout = new GridBagLayout(); 
	private JPanel suchFelder = new JPanel(suchFelderLayout);
	
	private JTextField suchEingabe = new JTextField();
	
	private JButton suchButton = new JButton("Suchen");
	
	private JList kundenListe = new JList();
	
	public KundenSuchLeiste(MainFrame owner){
		super();
		
		this.owner = owner;
		this.setTitle("Kunden suchen");
		
		suchEingabe.setToolTipText("Kundennummer oder Name");
		
		kundenListe.setCellRenderer(new KundenListenZeichner());
		
		
		initializeLayout();
		this.pack();
		this.setResizable(true);
		this.setMinimumSize(this.getSize());

	}
	
	private void initializeLayout(){
		
		this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
		
		GridBagConstraints constraint = new GridBagConstraints();
		constraint.fill = GridBagConstraints.BOTH;
		constraint.gridx=0;
		constraint.gridy=0;
		constraint.gridwidth = 3;
		constraint.weightx = 1.0;
		suchFelder.add(suchEingabe,constraint);
		
		constraint.gridx=3;
		constraint.gridwidth = 1;
		constraint.weightx = 0.0;
		suchFelder.add(suchButton,constraint);
		
		suchButton.addActionListener(new ButtonListener(owner, owner,owner, ButtonListener.Action.Suche_Kunden));
		
		this.add(suchFelder);

		kundenListe.setListData(new Vector<Kunde>());

		kundenListe.setVisibleRowCount(20);
		JScrollPane scroll = new JScrollPane(kundenListe,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		
		kundenListe.addMouseListener(new KundenListenMausListener(this.owner, kundenListe));
		
		this.add(scroll);
	}
	
	@Override
	public void doLayout() {
	
		this.setPreferredSize(this.getSize());
		super.doLayout();
	}
	
	
	public void zeigeKundenListe(java.util.List<Kunde> liste){
		
		kundenListe.setListData(liste.toArray(new Kunde[0]));
		kundenListe.validate();
	}
	

	@Override
	public String liesKundenSuche() {
		return suchEingabe.getText();
	}
}
